//
//  AppDelegate.swift
//  weiboxiangmu
//
//  Created by wei_jingyun on 15/6/23.
//  Copyright © 2015年 wei_jingyun. All rights reserved.
//

import UIKit
import SVProgressHUD
import AFNetworking
import SDWebImage

 /// 提示信息
let JYNetError = SVProgressHUD.showInfoWithStatus("您的网络不给力或服务器拒绝访问") as Any
let JYdisMiss = SVProgressHUD.dismiss() as Any
let JYShow = SVProgressHUD.show() as Any

/// 全局变量记录用户账号
var sharedUserAccount = UserAccount.loadAccount()
/// 切换根控制器的通知
let JYSwitchRootVCNotification = "JYSwitchRootVCNotification"

// @UIApplicationMain 是程序的入口
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        setupAppearance()
        setupNetWork()
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.rootViewController = defaultViewController()
        window?.makeKeyAndVisible()

        // 注册通知 单例类无需释放
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchViewController:", name: JYSwitchRootVCNotification, object: nil)
        return true
    }
    
   
    // MARK: - 控制器切换 
    func switchViewController(n: NSNotification){
        let isMainVc = n.object as! Bool
        window?.rootViewController = isMainVc ? mainViewController(): WelcomeViewController()
    }
    
    func defaultViewController() -> UIViewController {
        // TODO: 为调试修改
//        if sharedUserAccount != nil {
//            return WelcomeViewController()
//        }
        return mainViewController()
    }
    
    
    // MARK: - 设置外观
    private func setupAppearance() {
        // 设置外观，一经设置全局有效，必须早，通常会看到 appDelegate 中存在设置外观的代码
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        UITabBar.appearance().tintColor = UIColor.orangeColor()
    }
    
    //  MARK: - 设置网络
    private func setupNetWork(){
        // 设置网络指示器AFN
        AFNetworkActivityIndicatorManager.sharedManager().enabled = true
        // 网络缓存设置 路径设置为空会放在默认缓存区
        let urlCache = NSURLCache(memoryCapacity: 4 * 1024 * 1024, diskCapacity: 20  * 1024 * 1024, diskPath: nil)
        NSURLCache.setSharedURLCache(urlCache)
        

    }
    

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

